package cn.ssm.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ssm.biz.impl.ClassroomBizImpl;
import cn.ssm.biz.impl.CompInfoBizImpl;
import cn.ssm.biz.impl.ComputerroomBizImpl;
import cn.ssm.biz.impl.FaultBizImpl;
import cn.ssm.biz.impl.RepairListBizImpl;
import cn.ssm.biz.impl.TeacherBizImpl;
import cn.ssm.pojo.Class;
import cn.ssm.pojo.Computerroom;
import cn.ssm.pojo.Fault;
import cn.ssm.pojo.Pager;
import cn.ssm.pojo.Repairlist;
import cn.ssm.pojo.Teacher;
import cn.ssm.pojo.User;
import cn.ssm.query.RepairlistQuery;

/**
 * 查看维修
 * 
 * @author shi
 *
 */
@Controller
@RequestMapping("/repairList")
public class RepairListController {
	@Autowired
	@Qualifier("computerroomBizImpl")
	private ComputerroomBizImpl computerroomBizImpl;

	
	@Autowired
	@Qualifier("computerroomBizImpl")
	private ComputerroomBizImpl com;
	
	@Autowired
	@Qualifier("FaultBizImpl")
	private FaultBizImpl faultBizImpl;

	@Autowired
	@Qualifier("CImpl")
	private CompInfoBizImpl CompInfoBizImpl;

	@Autowired
	@Qualifier("RImpl")
	private RepairListBizImpl repBizimpl;

	@Autowired
	@Qualifier("TeacherBizImpl")
	private TeacherBizImpl teacherBizImpl;

	@Autowired
	@Qualifier("ClassroomBizImpl")
	private ClassroomBizImpl classroomBizImpl;

	
	
	
	@RequestMapping("/addrapairlist")
	public String addRapairlist(HttpSession session,ModelMap mo, Repairlist r,
			@RequestParam("computerId") int computerId, @RequestParam("computerroomId") int computerroomId,@RequestParam("faultName") String faultName ) {
		int num = 0;
		User user=(User)session.getAttribute("user");
		int userId=user.getId();
		System.out.println("===================="+userId);
		r.setUserId(userId);
		System.out.println("--------------------------------------进入addrapairlist---------------------------------------------------------------");
		String url = "";
		

		
		//获取faultName 名字然后截取，id和名字
		String[] split = faultName.split("_");
		Integer faultId = Integer.parseInt(split[0]);
		String faultType = split[1];

		
		
		//判断是否重复提交的代码
		Integer selRepairlistIsOne = repBizimpl.selRepairlistIsOne(faultId, computerroomId, computerId);
		
	
		if(selRepairlistIsOne>=1) {
			
			return "forward:/repairList/findR2?mes=此故障已被提交&computerId="+computerId+"&computerroomId="+computerroomId;
		}
		
		
		r.setFaultId(faultId);
		
		if (faultType.equals("公共设备")) {
			r.setComputerId(0);
			num = repBizimpl.addrepairlist(r);
		} else {
			num = repBizimpl.addrepairlist(r);
		}

		num = CompInfoBizImpl.updateCompinfoDescribeById("损坏", computerId, computerroomId);
		num = computerroomBizImpl.updateRoomDescribeById("损坏", computerroomId);
		if (num > 0) {
			url = "forward:/repairList/findR2?computerId="+computerId+"&computerroomId="+computerroomId;
		} else {
			url = "404";
		}
		
	
		return url;
	}
	
	
	
	/**
	 * 查看机房的全部信息
	 * @param mo
	 * @param r
	 * @param computerId
	 * @param computerroomId
	 * @param faultName
	 * @return
	 */
	@RequestMapping("/addrapairlist2")
	public String addRapairlist2(HttpSession session,ModelMap mo, Repairlist r,
			@RequestParam("computerId") int computerId, @RequestParam("computerroomId") int computerroomId,@RequestParam("faultName") String faultName ) {
		int num = 0;
	
		System.out.println("--------------------------------------进入addrapairlist---------------------------------------------------------------");
		String url = "";
		
		User user=(User)session.getAttribute("user");
		int userId=user.getId();
		System.out.println("===================="+userId);
		r.setUserId(userId);
		//获取faultName 名字然后截取，id和名字
		String[] split = faultName.split("_");
		Integer faultId = Integer.parseInt(split[0]);
		String faultType = split[1];
		
		r.setFaultId(faultId);
	
		//判断是否重复提交的代码
		Integer selRepairlistIsOne = repBizimpl.selRepairlistIsOne(faultId, computerroomId, computerId);
		
		if(selRepairlistIsOne>=1) {
			
			return "forward:/repairList/findR?mes=此故障已被提交";
		}
		
	
		if (faultType.equals("公共设备")) {
			r.setComputerId(0);
			num = repBizimpl.addrepairlist(r);
		} else {
			num = repBizimpl.addrepairlist(r);
		}

		
		
		num = CompInfoBizImpl.updateCompinfoDescribeById("损坏", computerId, computerroomId);
		num = computerroomBizImpl.updateRoomDescribeById("损坏", computerroomId);
		if (num > 0) {
			url = "forward:/repairList/findR";
		} else {
			url = "404";
		}
		
		
	
		
		return url;
	}
	
	
	
	

	
	@RequestMapping("/findAddList2")
	public String findAddList2(ModelMap mo,
			@RequestParam("computerroomId") int computerroomId) {
		System.out.println("========================进入===============================");
		String periodName = "";
	
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String gmtString = format.format(new java.util.Date());
		
		
		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		List<Fault> faultList = faultBizImpl.faultInfo1();
		mo.put("teacherList", teacherList);
		mo.put("classroomInfo", classroomInfo);
		mo.put("computerroomId", computerroomId);
		mo.put("faultList", faultList);
		
		
		mo.put("faultTime",gmtString);
		System.out.println("==========faultList===========" + faultList.size());

		// mo.put("selComputer",selComputer);
		return "addicheck2";

	}
	
	
	
	
	@RequestMapping("/findR") // 這個是展示列表的
	public String findRList(ModelMap mo, HttpServletRequest request,
			@RequestParam("computerroomId") int computerroomId,String mes) {

		String currentPageStr = request.getParameter("currentPage");
		// System.out.println(currentPageStr);
		int rowCount = repBizimpl.selRepairlistComputerroomIdCount(computerroomId);
		
		
		
		Pager pager = null;
		if (currentPageStr == null) {
			pager = new Pager(rowCount, 5, 1);
		} else {
			int currentPage = Integer.parseInt(request.getParameter("currentPage"));
			pager = new Pager(rowCount, 5, currentPage);
		}
		pager.setParams("?computerroomId="+computerroomId);
		pager.setUrl("/repairList/findR");
		List<RepairlistQuery> r = repBizimpl.findRepairlistQuery(pager, computerroomId);
		mo.addAttribute("pager", pager);
		mo.put("rlist", r);
		mo.put("computerroomId", computerroomId);
		mo.put("mes", mes);

		
		return "icheck";

	}
	
	
	

	// 模态框跳转查询该电脑所有故障页面
	@RequestMapping("/findR2")
	public String findRList2(ModelMap mo, HttpServletRequest request, Integer computerId, Integer computerroomId,String mes) {

		
		
		String currentPageStr = request.getParameter("currentPage");
		// System.out.println(currentPageStr);
		
		int rowCount = repBizimpl.selRepairlistQueryFaultNameCount(computerId,computerroomId);
		
		
		Pager pager = null;
		if (currentPageStr == null) {
			pager = new Pager(rowCount, 5, 1);
		} else {
			int currentPage = Integer.parseInt(request.getParameter("currentPage"));
			pager = new Pager(rowCount, 5, currentPage);
		}
		pager.setParams("?computerId="+computerId+"&computerroomId="+computerroomId);
		pager.setUrl("/repairList/findR2");
		
		
		List<RepairlistQuery> r = null;
	
		r = repBizimpl.findRepairlistQueryFaultName(pager, computerId, computerroomId);
		
		mo.put("rlist", r);
		mo.put("pager", pager);
		mo.put("computerId", computerId);
		mo.put("computerroomId", computerroomId);
		mo.put("mes", mes);


		return "icheck2";

	}

	
	
	
	@RequestMapping("/rId")
	// 根据id查询 修改用的
	public String findRListId(ModelMap mo, int id, Integer computerId, Integer computerroomId) {
		System.out.println("------------------------------fffff-------------------------");
		Repairlist r = repBizimpl.findRepairlistId(id);
		System.out.println(id + "id");
		// 新代碼
		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		List<Computerroom> computerroom=com.getRoomdescribe();
		List<Fault> faultList=faultBizImpl.faultInfo1();
		mo.put("faultList", faultList);
		mo.put("teacherList", teacherList);
		mo.put("classroomInfo", classroomInfo);
		mo.put("r", r);
		mo.put("computerId", computerId);
		mo.put("computerroomId", computerroomId);
		mo.put("computerroom", computerroom);
		
		return "upicheck";

	}
	
	
	
	/**
	 * 跳转到显示机房全部损坏信息的页面
	 * @param mo
	 * @param id
	 * @param computerId
	 * @param computerroomId
	 * @return
	 */
	@RequestMapping("/rId2")
	// 根据id查询 修改用的
	public String findRListId2(ModelMap mo, int id, Integer computerId, Integer computerroomId) {
		System.out.println("------------------------------fffff-------------------------");
		Repairlist r = repBizimpl.findRepairlistId(id);
		System.out.println(id + "id");
		// 新代碼
		List<Teacher> teacherList = teacherBizImpl.getTeacherList1();
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		List<Computerroom> computerroom=com.getRoomdescribe();
		List<Fault> faultList=faultBizImpl.faultInfo1();
		mo.put("faultList", faultList);

		mo.put("teacherList", teacherList);
		mo.put("classroomInfo", classroomInfo);
		mo.put("r", r);
		mo.put("computerId", computerId);
		mo.put("computerroomId", computerroomId);
		mo.put("computerroom", computerroom);

		return "upicheck2";

	}

	
	
	
	@RequestMapping("/upId")
	// 根据id修改
	public String updatelId(int id, int computerId, int faultId, Date faultTime,
			@RequestParam("computerroomId") int computerroomId, int classroomId, int teacherId, int statusId) {
		Repairlist rel = new Repairlist();
		System.out.println(
				"=========================================================================================================");
		System.out.println("进入修改");
		System.out.println(id);
		System.out.println(computerId);
		System.out.println(faultId);
		System.out.println(computerroomId);
		System.out.println(classroomId);
		System.out.println(teacherId);
		System.out.println(statusId);
		System.out.println(faultTime);
		System.out.println("获取  页面数据成功！  进行修改值!");

		rel.setId(id);
		rel.setClassroomId(classroomId);
		rel.setComputerId(computerId);
		rel.setTeacherId(teacherId);
		rel.setStatusId(statusId);
		rel.setFaultId(faultId);
		rel.setFaultTime(faultTime);
		rel.setComputerroomId(computerroomId);

		int num = repBizimpl.updateLId(rel);
		List<RepairlistQuery> status = repBizimpl.selectOneComputerStatusByCId(computerId, computerroomId);
		if (num > 0) {
			boolean temp = true;
			for (RepairlistQuery s : status) {
				if (s.getStatusId() != 3) {
					temp = false;
				}
			}
			if(temp==true){
				CompInfoBizImpl.updateCompinfoDescribeById("正常", computerId, computerroomId);
			}else if (temp==false) {
				CompInfoBizImpl.updateCompinfoDescribeById("损坏", computerId, computerroomId);

			}
		} else {
			System.out.println("修改失败!");
		}

		List<RepairlistQuery> roomStatus = repBizimpl.selectroomStatusByroomId(computerroomId);
		boolean falg = true;
		for (RepairlistQuery rs : roomStatus) {
			if (rs.getStatusId() != 3) {
				falg = false;
			}

		}
		if (falg==true) {
			computerroomBizImpl.updateRoomDescribeById("正常", computerroomId);
		
		}else if (falg==false){
			computerroomBizImpl.updateRoomDescribeById("损坏", computerroomId);
		}
		
		return "forward:/repairList/findR2";
	}
	
	
	
	/**
	 * 修改后跳转机房全部信息
	 * @param id
	 * @param computerId
	 * @param faultId
	 * @param faultTime
	 * @param computerroomId
	 * @param classroomId
	 * @param teacherId
	 * @param statusId
	 * @return
	 */
	@RequestMapping("/upId2")
	// 根据id修改
	public String updatelId2(int id, int computerId, int faultId, Date faultTime,
			@RequestParam("computerroomId") int computerroomId, int classroomId, int teacherId, int statusId) {

	
		Repairlist rel = new Repairlist();
		System.out.println(
				"=========================================================================================================");
		System.out.println("进入修改");
		System.out.println(id);
		System.out.println(computerId);
		System.out.println(faultId);
		System.out.println(computerroomId);
		System.out.println(classroomId);
		System.out.println(teacherId);
		System.out.println(statusId);
		System.out.println(faultTime);
		System.out.println("获取  页面数据成功！  进行修改值!");

		rel.setId(id);
		rel.setClassroomId(classroomId);
		rel.setComputerId(computerId);
		rel.setTeacherId(teacherId);
		rel.setStatusId(statusId);
		rel.setFaultId(faultId);
		rel.setFaultTime(faultTime);
		rel.setComputerroomId(computerroomId);

		int num = repBizimpl.updateLId(rel);
		List<RepairlistQuery> status = repBizimpl.selectOneComputerStatusByCId(computerId, computerroomId);
		if (num > 0) {
			boolean temp = true;
			for (RepairlistQuery s : status) {
				if (s.getStatusId() != 3) {
					temp = false;
				}
			}
			if(temp==true){
				CompInfoBizImpl.updateCompinfoDescribeById("正常", computerId, computerroomId);
			}else if (temp==false) {
				CompInfoBizImpl.updateCompinfoDescribeById("损坏", computerId, computerroomId);

			}
		} else {
			System.out.println("修改失败!");
		}

		List<RepairlistQuery> roomStatus = repBizimpl.selectroomStatusByroomId(computerroomId);
		boolean falg = true;
		for (RepairlistQuery rs : roomStatus) {
			if (rs.getStatusId() != 3) {
				falg = false;
			}

		}
		if (falg==true) {
			computerroomBizImpl.updateRoomDescribeById("正常", computerroomId);
		
		}else if (falg==false){
			computerroomBizImpl.updateRoomDescribeById("损坏", computerroomId);
		}
		
		
		return "forward:/repairList/findR";
	}
	
	
	

	@RequestMapping("delId")
	public String delRid(Integer computerId, Integer computerroomId,int id) {
		
		
		int num = repBizimpl.delId(id);
		List<RepairlistQuery> status = repBizimpl.selectOneComputerStatusByCId(computerId, computerroomId);
		if (num > 0) {
			boolean temp = true;
			for (RepairlistQuery s : status) {
				if (s.getStatusId() != 3) {
					temp = false;
				}
			}
			if(temp==true){
				CompInfoBizImpl.updateCompinfoDescribeById("正常", computerId, computerroomId);
			}else if (temp==false) {
				CompInfoBizImpl.updateCompinfoDescribeById("损坏", computerId, computerroomId);

			}
		} else {
			System.out.println("修改失败!");
		}

		List<RepairlistQuery> roomStatus = repBizimpl.selectroomStatusByroomId(computerroomId);
		boolean falg = true;
		for (RepairlistQuery rs : roomStatus) {
			if (rs.getStatusId() != 3) {
				falg = false;
			}

		}
		if (falg==true) {
			computerroomBizImpl.updateRoomDescribeById("正常", computerroomId);
		
		}else if (falg==false){
			computerroomBizImpl.updateRoomDescribeById("损坏", computerroomId);
		}
		return "forward:/repairList/findR2";
		
	}
	
	
	/**
	 * 跳转到机房的全部损坏信息
	 * @param computerId
	 * @param computerroomId
	 * @param id
	 * @return
	 */
	@RequestMapping("delId2")
	public String delRid2(Integer computerId, Integer computerroomId,int id) {
		int num = repBizimpl.delId(id);
		List<RepairlistQuery> status = repBizimpl.selectOneComputerStatusByCId(computerId, computerroomId);
		if (num > 0) {
			boolean temp = true;
			for (RepairlistQuery s : status) {
				if (s.getStatusId() != 3) {
					temp = false;
				}
			}
			if(temp==true){
				CompInfoBizImpl.updateCompinfoDescribeById("正常", computerId, computerroomId);
			}else if (temp==false) {
				CompInfoBizImpl.updateCompinfoDescribeById("损坏", computerId, computerroomId);

			}
		} else {
			System.out.println("修改失败!");
		}

		List<RepairlistQuery> roomStatus = repBizimpl.selectroomStatusByroomId(computerroomId);
		boolean falg = true;
		for (RepairlistQuery rs : roomStatus) {
			if (rs.getStatusId() != 3) {
				falg = false;
			}

		}
		if (falg==true) {
			computerroomBizImpl.updateRoomDescribeById("正常", computerroomId);
		
		}else if (falg==false){
			computerroomBizImpl.updateRoomDescribeById("损坏", computerroomId);
		}
			
		return "forward:/repairList/findR";
	}
	
	
	

	@RequestMapping("findFaultCount")
	public String findFaultCount(@RequestParam("computerroomId") int computerroomId,
			@RequestParam("faultId") int faultId, @RequestParam("statusId") int statusId) {

		return "forward:findR";

	}

}
